<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head th:include="include :: header"></head>
<body class="hold-transition skin-blue layout-top-nav">
<input type="hidden" id="id">
<input type="hidden" id="days">
<!--文件列表模态框-->
<div class="modal fade" id="modal-import-record">
    <div class="modal-dialog" style="width: 1240px;">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title">当日生产产量统计明细</h4>
            </div>
            <div class="modal-body">
                <div class="row">
                    <table id="bootstrap-table-explain"></table>
                </div>
            </div>
        </div>
        <!-- /.modal-content -->
    </div>
    <!-- /.modal-dialog -->
</div>
<div class="wrapper">
    <header class="main-header">
    </header>
    <div class="content-wrapper">
        <div class="container">
            <section class="content" style="margin-top: 30px">
                <div class="nav-tabs-custom">
                    <ul class="nav nav-tabs pull-right">
                        <li><a id="calendar-li" href="#tab_1-1" data-toggle="tab">图表</a></li>
                        <!--<li class="active"><a href="#tab_2-2" data-toggle="tab">表格</a></li>-->
                    </ul>
                    <div class="tab-content">
                        <div class="tab-pane active" id="tab_1-1">
                            <div class="row">
                                <div class="col-md-12">
                                    <div id="calendar"></div>
                                </div>
                            </div>

                        </div>
                    </div>
                </div>
            </section>
        </div>
    </div>
</div>
<div th:include="include :: footer"></div>
<script th:inline="javascript">

    // var ctx = ctx + 'reportHistory/';
    var statusString = ['撤销', '提交待审', '审阅待批', '审批通过', '审批驳回'];
    var colorString = ['text-info', 'text-info', 'text-primary', 'text-green', 'text-red'];
    let table;
    $(function () {
        js.post({
            url: ctx + "product/detail/infoDetail",
            data: {time: [[${time}]]},
            success: function (result) {
                if (result.type === web_status.SUCCESS) {
                    var productModelList = result.data.rows;
                    var sources = [];
                    for (let i = 0; i < productModelList.length; i++) {
                        if (productModelList[i].type == 5) {
                            break;
                        }
                        if (productModelList[i].solderCompleteNum != "铆焊完成重量：0.00") {

                            var dd = {};
                            dd.title = productModelList[i].solderCompleteNum;
                            dd.start = new Date(productModelList[i].staTime);
                            dd.end = new Date(productModelList[i].enTime);
                            dd.className = 'text22';
                            dd.backgroundColor = '#00c0ef';
                            // dd.url='https://www.baidu.com';
                            sources.push(dd);
                        }
                        if (productModelList[i].machiningCompleteNum != "机加完成工时：0.00") {
                            dd = {};
                            dd.title = productModelList[i].machiningCompleteNum;
                            dd.start = new Date(productModelList[i].staTime);
                            dd.end = new Date(productModelList[i].enTime);
                            dd.className = 'text22';
                            dd.backgroundColor = '#129EDE';
                            // dd.url='https://www.chinaso.com/';
                            sources.push(dd);
                        }
                        if (productModelList[i].sprayCompleteNum != "喷涂完成重量：0.00") {
                            dd = {};
                            dd.title = productModelList[i].sprayCompleteNum;
                            dd.start = new Date(productModelList[i].staTime);
                            dd.end = new Date(productModelList[i].enTime);
                            dd.className = 'text22';
                            dd.backgroundColor = '#239409';
                            // dd.url='https://www.csdn.net/';
                            sources.push(dd);
                        }
                    }


                    $('#calendar').fullCalendar({
                        header: {
                            // right: 'prev next today'
                            right: ''
                        },
                        buttonText: {
                            today: '今天'
                        },
                        defaultDate: [[${time}]],
                        displayEventTime: false,
                        showNonCurrentDates: false,
                        fixedWeekCount: false,
                        events: sources,
                        editable: false,
                        droppable: false,
                        selectable: true,
                        selectHelper: true,
                        select: function (start, end) {
                            //弹出模态框
                            var staTime = getDate(start);//日期格式转换
                            // 加载项目说明文件列表
                            initTableExplain(staTime);
                            js.modal.open("modal-import-record");
                            // window.open('https://www.taobao.com');
                        },
                        // eventClick: function(event) {
                        //
                        //     alert(event.url);
                        //     // window.open(event.url, 'gcalevent', 'width=700,height=600');
                        //     return false;
                        // },
                    });
                } else {
                    js.modal.warning(result.msg);
                }
            }
        })

         function getDate(dt) {
            // dt.setHours(dt.getHours())
            var now = new Date(dt);
            var y = now.getFullYear();
            var m = now.getMonth()+1;
            var d = now.getDate();
            return y + '-' + (m < 10 ? '0' + m : m) + '-' + (d < 10 ? '0' + d : d)
        }


        $("#calendar-li").on('shown.bs.tab', function () {
            $("#calendar").fullCalendar('render');
        });

        // 日期控件初始化
        $(".d-p").datepicker({
            format: 'yyyy-mm-dd',
            language: 'zh-cn',
            startDate: new Date().format("yyyy-mm"),
            autoclose: true,
            clearBtn: true
        });

        // 返回时间改变事件
        $(".d-p").change(function () {
            var sd = $("#startTime").val();
            var ed = $(this).val();
            if (sd && ed) {
                $("#days").val($.common.diffDay(sd, ed));
            } else {
                $("#days").val('');
            }
        });

    });

    // 返回确认模态框
    function backSubmit(id, reportDays, startTime, endTime) {
        $("#id").val(id);
        $("#days").val(reportDays);
        $("#startTime").val(startTime);
        $("#endTime").val('');
        $.modal.open("modal-backSubmit");
    }
    /** 关闭装配零件信息选择模态框 */
    js.modal.hideEvent("modal-backSubmit", function () {
        js.table.destroy(table);
    });
    // 撤销操作
    function cancel(id) {
        $.operate.post(ctx + "cancel", {id: id}, function () {
            // 刷新表格
            $.table.refresh();
        }, true);
    }

    /** 文件附件列表 */
    function initTableExplain(time) {
        $('#bootstrap-table-explain').bootstrapTable('destroy');//这里必须要添加这个销毁，否则新增、修改、查看的切换可编辑列表中的数据可能加载出现问题。
        // 加载表格
        table = js.table.init({
            url: ctx + "projectScheduling/productDetailList",
            id: "bootstrap-table-explain",
            queryData: {
                time: time
            },
            showExport: false,
            pagination: false,
            onLoadSuccess: function (data) {
                mergeCells(data, "route", "route", 1, $('#bootstrap-table-explain')); //合并单元格
                mergeCells(data, "route", "number", 1, $('#bootstrap-table-explain')); //合并单元格
            },
            columns: [
                // {
                //
                //     title: '序号', field: 'id', width: '50',
                //     formatter: function (value, row, index) {
                //         return index + 1;
                //     }
                // },
                {title: "统计产量类型", field: 'route'},
                {title: "当日完成量", field: 'number'},
                {title: "完成车间", field: 'recDept'},
                {title: "完成项目", field: 'prodNo'},
            ]
        });
    }
    /**
     * 合并单元格
     * @param data 原始数据（在服务端完成排序）
     * @param fieldName 合并依据属性名称
     * @param spanFieldName 合并属性名称
     * @param colspan 合并列
     * @param target 目标表格对象
     */
    function mergeCells(data, fieldName, spanFieldName, colspan, target) {
        //声明一个map计算相同属性值在data对象出现的次数和
        var sortMap = {};
        for (var i = 0; i < data.rows.length; i++) {
            for (var prop in data.rows[i]) {
                if (prop == fieldName) {
                    var key = data.rows[i][prop]
                    if (sortMap.hasOwnProperty(key)) {
                        sortMap[key] = sortMap[key] * 1 + 1;
                    } else {
                        sortMap[key] = 1;
                    }
                    break;
                }
            }
        }
        var index = 0;
        for (var prop in sortMap) {
            var count = sortMap[prop] * 1;
            $(target).bootstrapTable('mergeCells', {
                index: index,
                field: spanFieldName,
                colspan: colspan,
                rowspan: count
            });
            index += count;
        }
    }

</script>
</body>
</html>
